package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.MessageConstant;
import org.example.demo.entity.UserMeeting;
import org.example.demo.service.UserMeetingService;
import org.example.demo.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户会议关联控制器
 */
@RestController
@RequestMapping("userMeeting")
public class UserMeetingController {

    @Resource
    private UserMeetingService userMeetingService;

    /**
     * 分页查询用户会议关联
     * 
     * @param userMeeting 查询条件
     * @param pageNum     页码
     * @param pageSize    每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody UserMeeting userMeeting,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<UserMeeting> page = userMeetingService.selectPage(userMeeting, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询用户会议关联
     * 
     * @param id 关联ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        UserMeeting userMeeting = userMeetingService.getById(id);

        if (userMeeting != null) {
            return AjaxResult.success(userMeeting);
        } else {
            return AjaxResult.error("用户会议关联不存在");
        }
    }

    /**
     * 新增用户会议关联
     * 
     * @param userMeeting 用户会议关联信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody UserMeeting userMeeting) {
        if (userMeetingService.save(userMeeting)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 修改用户会议关联
     * 
     * @param userMeeting 用户会议关联信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody UserMeeting userMeeting) {
        if (userMeetingService.updateById(userMeeting)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除用户会议关联
     * 
     * @param id 关联ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (userMeetingService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据用户ID查询其参与的所有会议ID
     *
     * @param userId 用户ID
     * @return 会议ID列表
     */
    @GetMapping("meetingIds/{userId}")
    public AjaxResult getMeetingIdsByUserId(@PathVariable Long userId) {
        List<Long> meetingIds = userMeetingService.getMeetingIdsByUserId(userId);
        return AjaxResult.success(meetingIds);
    }

    /**
     * 根据会议ID查询所有参会用户ID
     *
     * @param meetingId 会议ID
     * @return 用户ID列表
     */
    @GetMapping("userIds/{meetingId}")
    public AjaxResult getUserIdsByMeetingId(@PathVariable Long meetingId) {
        List<Long> userIds = userMeetingService.getUserIdsByMeetingId(meetingId);
        return AjaxResult.success(userIds);
    }

    /**
     * 批量保存用户与会议的关联关系
     *
     * @param meetingId 会议ID
     * @param userIds   用户ID列表
     * @return 操作结果
     */
    @PostMapping("batch/{meetingId}")
    public AjaxResult saveBatch(@PathVariable Long meetingId, @RequestBody List<Long> userIds) {
        if (userMeetingService.saveBatch(meetingId, userIds)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除用户与会议的关联关系
     *
     * @param meetingId 会议ID
     * @param userId    用户ID
     * @return 操作结果
     */
    @DeleteMapping("{meetingId}/{userId}")
    public AjaxResult remove(@PathVariable Long meetingId, @PathVariable Long userId) {
        if (userMeetingService.remove(meetingId, userId)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }
}